package controller;

import java.awt.Container;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JTable;
import javax.swing.table.AbstractTableModel;

import model.Zimmer;
import view.RoomsListView;

public class GuiRoomsListController extends GUIAbstractController implements ActionListener
{
	private RoomsListView roomsView;
	private RoomsTableModel roomsModel;
	private Container container;
	
	public GuiRoomsListController(Container container){
		
		this.container = container;
		
		roomsView = new RoomsListView();
		roomsView.addActionListener(this);
		
		reloadRoomsTable();
	}

	private void reloadRoomsTable()
	{
		List<Zimmer> rooms = super.dbController.loadAllEntities(new Zimmer());
		JTable table = roomsView.getTableRooms();
		roomsModel = new RoomsTableModel(rooms);
		
		table.setModel(roomsModel);
		table.repaint();
	}

	public RoomsListView getRoomsView()
	{
		return roomsView;
	}

	@Override
	public void actionPerformed(ActionEvent e)
	{
		int index = roomsView.getTableRooms().getSelectedRow();
		Zimmer selectedRoom = roomsModel.getRooms().get(index);
		
		if(e.getSource() == roomsView.getBtnEdit()){
			
			GUICreateRoomViewController roomEditViewController = new GUICreateRoomViewController(selectedRoom);
			container.removeAll();
			container.add(roomEditViewController.getRoomView());
			container.setSize(roomEditViewController.getRoomView().getSize());
			container.validate();
			container.repaint();
		}else if(e.getSource() == roomsView.getBtnDelete()){
			super.dbController.deleteEntity(selectedRoom);
			reloadRoomsTable();
		}
		
	}
	
	
}